package com.wardrobe.soa.backend.booking_order;

import com.wardrobe.dao.address.AddressDao;
import com.wardrobe.dao.booking.BookingDao;
import com.wardrobe.dao.booking_order.BookingOrderDao;
import com.wardrobe.dao.shop.ShopDao;
import com.wardrobe.domain.address.AddressBean;
import com.wardrobe.domain.booking.BookingShowBean;
import com.wardrobe.domain.booking_order.BookingOrderFromBean;
import com.wardrobe.domain.booking_order.BookingOrderShowBean;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.shop.ShopShowBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/11/23.
 */
@Service
public class BackBookingOrderService {
    @Autowired
    private BookingOrderDao bookingOrderDao;
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private BookingDao bookingDao;
    @Autowired
    private ShopDao shopDao;

    /**
     * 预约订单
     * @param fromBean
     * @return
     */
    public List<BackBookingOrderResponse> getBookingOrderList(BackBookingOrderFromBean fromBean){
        List<BookingOrderShowBean> bookingOrderList = bookingOrderDao.selectBookingOrderList(fromBean);
        List<BackBookingOrderResponse> list = new ArrayList<>();
        bookingOrderList.stream().forEach(x ->{
            AddressBean addressBean = addressDao.selectDefaultAddress(x.getUserId());
            BookingShowBean bookingShowBean = bookingDao.selectBookingById(x.getBookingId());
            ShopShowBean shopShowBean = shopDao.selectShopById(x.getShopId());
            if(addressBean==null){
                addressBean = new AddressBean();
                addressBean.setName("");
                addressBean.setAddress("");
                addressBean.setMobileNumber("");
            }
            list.add(BackBookingOrderResponse.builder()
                    .id(x.getId())
                    .userId(x.getUserId())
                    .shopId(x.getShopId())
                    .orderId(x.getOrderId())
                    .payPrice(x.getPayPrice())
                    .remark(x.getRemark())
                    .bookingTime(x.getBookingTime())
                    .status(x.getStatus())
                    .shopName(shopShowBean.getShopName())
                    .userName(addressBean.getName())
                    .bookingName(bookingShowBean.getName())
                    .address(addressBean.getAddress())
                    .telphone(addressBean.getMobileNumber())
                    .createTime(x.getCreateTime())
                    .payTime(x.getPayTime())
                    .build());
        });
        return list;
    }
    public long countBookigOrder(BackBookingOrderFromBean fromBean){
        return bookingOrderDao.countBookingOrder(fromBean);
    }
    public int modifyBookingOrder(BackEndFromBean fromBean){
        return bookingOrderDao.updateBookingOrder(fromBean);
    }
}
